package com.sws.hm.crontab;


import com.sws.hm.dao.SysinfoDao;
import com.sws.hm.dto.port;
import com.sws.hm.entity.Sys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class cronSys {

    @Value("${time.out}")
    private int timeOut;

    @Autowired
    private SysinfoDao dao;

    @Scheduled(cron = "${sys.schedule}")
    public void AnimalAechartsMapper() throws IOException, ParseException {

        int timeOut = 3000;  //超时应该在3钞以上
        List<String> list = dao.findIpAll();
        for (int j = 0; j < list.size(); j++) {
            String ip = list.get(j);

            boolean status = InetAddress.getByName(ip).isReachable(timeOut);     // 当返回值是true时，说明host是可用的，false则不可。

            if (status) {
                log.info("主机已建立连接。。。");
                dao.updateSysifsocket(ip, 0);
            } else {
                log.info("主机未建立连接！！！");
                dao.updateSysifsocket(ip, 1);
            }

        }

    }


}
